// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ai.states;

import java.util.HashMap;
import zombie.ai.State;
import zombie.audio.parameters.ParameterZombieState;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.characters.IsoZombie;
import zombie.core.Core;
import zombie.core.Rand;
import zombie.iso.IsoMovingObject;
import zombie.iso.objects.IsoDeadBody;
import zombie.iso.objects.IsoZombieGiblets;
import zombie.network.GameClient;
import zombie.network.GameServer;

public final class ZombieEatBodyState extends State {
    private static final ZombieEatBodyState _instance = new ZombieEatBodyState();

    public static ZombieEatBodyState instance() {
        return _instance;
    }

    @Override
    public void enter(IsoGameCharacter owner) {
        IsoZombie isoZombie = (IsoZombie)owner;
        isoZombie.setStateEventDelayTimer(Rand.Next(1800.0F, 3600.0F));
        isoZombie.setVariable("onknees", Rand.Next(3) != 0);
        if (isoZombie.getEatBodyTarget() instanceof IsoDeadBody) {
            IsoDeadBody isoDeadBody = (IsoDeadBody)isoZombie.eatBodyTarget;
            if (!isoZombie.isEatingOther(isoDeadBody)) {
                HashMap hashMap = owner.getStateMachineParams(this);
                hashMap.put(0, isoDeadBody);
                isoDeadBody.getEatingZombies().add(isoZombie);
            }

            if (GameClient.bClient && isoZombie.isLocal()) {
                GameClient.sendEatBody(isoZombie, isoZombie.getEatBodyTarget());
            }
        } else if (isoZombie.getEatBodyTarget() instanceof IsoPlayer && GameClient.bClient && isoZombie.isLocal()) {
            GameClient.sendEatBody(isoZombie, isoZombie.getEatBodyTarget());
        }
    }

    @Override
    public void execute(IsoGameCharacter owner) {
        IsoZombie isoZombie = (IsoZombie)owner;
        IsoMovingObject isoMovingObject = isoZombie.getEatBodyTarget();
        if (isoZombie.getStateEventDelayTimer() <= 0.0F) {
            isoZombie.setEatBodyTarget(null, false);
        } else if (!GameServer.bServer && !Core.SoundDisabled && Rand.Next(Rand.AdjustForFramerate(15)) == 0) {
            isoZombie.parameterZombieState.setState(ParameterZombieState.State.Eating);
        }

        isoZombie.TimeSinceSeenFlesh = 0.0F;
        if (isoMovingObject != null) {
            isoZombie.faceThisObject(isoMovingObject);
        }

        if (Rand.Next(Rand.AdjustForFramerate(450)) == 0) {
            isoZombie.getCurrentSquare()
                .getChunk()
                .addBloodSplat(isoZombie.x + Rand.Next(-0.5F, 0.5F), isoZombie.y + Rand.Next(-0.5F, 0.5F), isoZombie.z, Rand.Next(8));
            if (Rand.Next(6) == 0) {
                new IsoZombieGiblets(
                    IsoZombieGiblets.GibletType.B,
                    isoZombie.getCell(),
                    isoZombie.getX(),
                    isoZombie.getY(),
                    isoZombie.getZ() + 0.3F,
                    Rand.Next(-0.2F, 0.2F) * 1.5F,
                    Rand.Next(-0.2F, 0.2F) * 1.5F
                );
            } else {
                new IsoZombieGiblets(
                    IsoZombieGiblets.GibletType.A,
                    isoZombie.getCell(),
                    isoZombie.getX(),
                    isoZombie.getY(),
                    isoZombie.getZ() + 0.3F,
                    Rand.Next(-0.2F, 0.2F) * 1.5F,
                    Rand.Next(-0.2F, 0.2F) * 1.5F
                );
            }

            if (Rand.Next(4) == 0) {
                isoZombie.addBlood(null, true, false, false);
            }
        }
    }

    @Override
    public void exit(IsoGameCharacter owner) {
        IsoZombie isoZombie = (IsoZombie)owner;
        HashMap hashMap = owner.getStateMachineParams(this);
        if (hashMap.get(0) instanceof IsoDeadBody) {
            ((IsoDeadBody)hashMap.get(0)).getEatingZombies().remove(isoZombie);
        }

        if (isoZombie.parameterZombieState.isState(ParameterZombieState.State.Eating)) {
            isoZombie.parameterZombieState.setState(ParameterZombieState.State.Idle);
        }

        if (GameClient.bClient && isoZombie.isLocal()) {
            GameClient.sendEatBody(isoZombie, null);
        }
    }
}
